package com.mo.axion.handler.pending;

import com.dtp.core.thread.DtpExecutor;
import com.mo.axion.handler.config.HandlerThreadPoolConfig;
import com.mo.axion.handler.utils.GroupIdMappingUtils;
import com.mo.axion.support.utils.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * @author Mo
 * @createTime 2024/2/17 17:07
 * @description 存储每种消息类型与TaskPending的关系
 */
@Component
public class TaskPendingHolder {
    /**
     * 获取所有消费者组Id
     */
    private static List<String> groupIdList = GroupIdMappingUtils.getAllGroupIdList();

    @Autowired
    private ThreadPoolUtils threadPoolUtils;
    private Map<String, ExecutorService> poolsHolder = new HashMap<>(32);

    /**
     * 样例: axion.email.notice
     */
    @PostConstruct
    public void init() {
        for (String groupId : groupIdList) {
            DtpExecutor executor = HandlerThreadPoolConfig.getExecutor(groupId);
            threadPoolUtils.register(executor);
            poolsHolder.put(groupId, executor);
        }
    }

    /**
     * 根据groupId得到对应的线程池
     * @param groupId
     * @return
     */
    public ExecutorService route(String groupId) {
        return poolsHolder.get(groupId);
    }
}
